package com.kedacom.freedomLabel.label;

import android.support.annotation.NonNull;

import com.gprinter.command.EscCommand;
import com.gprinter.command.GpUtils;
import com.gprinter.command.LabelCommand;

import com.kedacom.freedomLabel.label.annotation.FontScale;
import com.kedacom.freedomLabel.label.builder.LabelParams;
import com.kedacom.freedomLabel.label.column.BarCodeLabelColumn;
import com.kedacom.freedomLabel.label.column.BitmapLabelColumn;
import com.kedacom.freedomLabel.label.column.QRCodeLabelColumn;
import com.kedacom.freedomLabel.label.column.TextLabelColumn;

import java.util.Vector;

/**
 * @author licheng
 * @since 2019/11/28 16:13
 */
public class GpLabel<T> extends Label<T> {

    private LabelCommand mLabelCommand;

    private T data;
    private int count;

    public GpLabel() {
        super();
    }

    @Override
    public void bind(@NonNull LabelParams<T> params) {
        super.bind(params);
        this.data = params.data;
        this.count = params.count;
        mLabelCommand.addSize(params.width, params.height);
    }

    @Override
    public void before() {
        mLabelCommand = new LabelCommand();
        mLabelCommand.addDirection(LabelCommand.DIRECTION.BACKWARD, LabelCommand.MIRROR.NORMAL);
        mLabelCommand.addReference(0, 0);
        mLabelCommand.addTear(EscCommand.ENABLE.ON);
        mLabelCommand.addCls();
    }

    @Override
    public void addText(@NonNull TextLabelColumn column, @FontScale int xScale, @FontScale int yScale) {
        LabelCommand.FONTMUL xFontScale = GpLabelConverts.getLabelFountMul(xScale);
        LabelCommand.FONTMUL yFontScale = GpLabelConverts.getLabelFountMul(yScale);
        mLabelCommand.addText(
                column.x, column.y,
                column.fontType, column.rotation,
                xFontScale, yFontScale,
                column.value
        );
    }

    @Override
    public void addQRCode(@NonNull QRCodeLabelColumn column) {
        mLabelCommand.addQRCode(
                column.x, column.y,
                column.eec, column.width,
                column.rotation,
                column.value
        );
    }

    @Override
    public void addBarCode(@NonNull BarCodeLabelColumn column) {
        mLabelCommand.add1DBarcode(
                column.x, column.y,
                column.barCodeType, column.height,
                column.readable, column.rotation,
                column.value
        );
    }

    @Override
    public void addBitmap(@NonNull BitmapLabelColumn column) {
        mLabelCommand.addBitmap(
                column.x, column.y,
                column.bitmapMode, column.width,
                column.value
        );
    }

    @Override
    public void after() {
        mLabelCommand.addSound(1, 100);
        mLabelCommand.addPrint(1, this.count);
    }

    @Override
    public byte[] encodeCommand() {
        after();
        Vector<Byte> data = mLabelCommand.getCommand();
        return GpUtils.ByteTo_byte(data);
    }

    public T getData() {
        return data;
    }
}